package com.faxsun.core.order.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.broadleafcommerce.common.payment.PaymentTransactionType;
import org.broadleafcommerce.core.payment.domain.PaymentTransaction;
import org.springframework.stereotype.Repository;

@Repository("fsRefundDao")
public class PaymentTransactionDaoImpl implements PaymentTransactionDao {

	@PersistenceContext(unitName = "blPU")
	protected EntityManager em;

	@SuppressWarnings("unchecked")
	@Override
	public List<PaymentTransaction> readAllRefundTransactions() {

		Query query = em
				.createQuery("select transaction from org.broadleafcommerce.core.payment.domain.PaymentTransaction transaction"
						+ " where transaction.type=:type order by transaction.date desc");
		query.setParameter("type", PaymentTransactionType.REFUND.getType());
		return query.getResultList();
	}

	@Override
	public PaymentTransaction save(PaymentTransaction transaction) {
		PaymentTransaction response = em.merge(transaction);
		return response;
	}

}
